/*
 * @Copyright: 2016 www.yyfax.com Inc. All rights reserved.
 */
package com.yyfax.pay.order.data.dao.impl;

import java.sql.SQLException;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;

import com.yyfax.framework.dao.impl.BaseDaoImpl;
import com.yyfax.pay.order.data.dao.PayinBatchOrderDao;
import com.yyfax.pay.order.model.po.PayinBatchOrderPO;
import com.yyfax.pay.order.model.po.PayinOrderPO;
import com.yyfax.pay.order.model.po.PayoutBatchOrderPO;

/**
 * 批量代付订单持久化实现
 *
 * @author maojian
 * @date 2017年04月12日 11:09:15
 * @since 1.0.0
 */
@Repository
public class PayinBatchOrderDaoImpl extends BaseDaoImpl<PayinBatchOrderPO, Integer> implements PayinBatchOrderDao {
	/** 表名，该值要和payout_batch_order.xml的属性"tableName"保持一致 */
	private static final String TABLE_NAME = "payin_batch_order";

	/** 方法名，该值要和payout_batch_order.xml的属性"id"保持一致 */
	private final String SQL_INSERT = "insert";

	private final String SQL_SELECT_BY_ORDERID_AND_MERCHANTCODE = "selectByOrderIdAndMerchantCode";
	
	private final String SQL_UPDATE = "update";
	
	public PayinBatchOrderDaoImpl() {
		super(TABLE_NAME);
	}

	@Override
	public int insertReturnPK(PayinBatchOrderPO po) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_INSERT);
		BeanPropertySqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(po);
		return namedParameterJdbcTemplate.insertForPK(sql, sqlParameterSource);
	}

	/**
	 * 根据orderId获取实体
	 * 
	 * @user maojian
	 * @date 2017-05-3 11:09:15
	 * @since 1.0.0
	 * @param id 自增主键
	 * @param merchantCode 商户编号(为分表预留参数)
	 * @return
	 */
	public PayinBatchOrderPO selectByOrderIdAndMerchantCode(String orderId, String merchantCode) throws Exception{
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_ORDERID_AND_MERCHANTCODE);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("orderId", orderId);
		params.addValue("merchantCode", merchantCode);
		return namedParameterJdbcTemplate.queryForObject(sql, params, new BeanPropertyRowMapper<PayinBatchOrderPO>(poClass));
	}
	
	@Override
	public int update(PayinBatchOrderPO po) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_UPDATE);
		BeanPropertySqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(po);
		return namedParameterJdbcTemplate.update(sql, sqlParameterSource);
	}


}
